/**
 * Copyright (C) 2009 Olafur Gauti Gudmundsson, Hjortur Stefan Olafsson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.gwt_control.client;

import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Olafur Gauti Gudmundsson
 */
public class PageLayout implements Serializable {

    protected final Map<String,List<Widget>> widgets;

    public PageLayout() {
        widgets = new HashMap<String,List<Widget>>();
    }

    public void clear() {
        Set<String> keys = new HashSet<String>(widgets.keySet());
        for ( String key : keys ) {
            clear(key);
        }
    }

    public void clear( String key ) {
        RootPanel.get(key).clear();
        widgets.remove(key);
    }

    public void addWidget( String key, Widget w ) {
        if ( !widgets.containsKey(key) ) {
            widgets.put(key, new ArrayList<Widget>());
        }
        widgets.get(key).add(w);
    }

    public List<Widget> getWidgets( String key ) {
        return widgets.get(key);
    }

    public void render() {
        for ( Map.Entry<String,List<Widget>> entry : widgets.entrySet() ) {
            for ( Widget w : entry.getValue() ) {
                RootPanel.get(entry.getKey()).add(w);
            }
        }
    }
}
